#!/bin/tcsh -efX
#############################################################################
#                                                                           #
# Title: AUX-Remove FREALIGN files	                    					#
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 29/07/2016                                             #
# Last Modification: 29/07/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 120
#
#
# MANUAL: This script allows the user to remove specific .par or .mrc files from a FREALIGN refinement.
#
# DISPLAY: SPR_DIR
# DISPLAY: SPR_PREREFINE_DIR
# DISPLAY: SPR_REFINE_DIR
# DISPLAY: SPR_FRLRefOrPreref
# DISPLAY: SPR_WhichStackAll
# DISPLAY: SPR_FRLCycle
# DISPLAY: SPR_FRLRef
# DISPLAY: SPR_FRLRemovePar
# DISPLAY: SPR_FRLRemoveMrc
# DISPLAY: SPR_CleanSPR
#
#$end_local_vars

set dir_frealign = ""

set scriptname = RemoveFrealignParMrc

set SPR_DIR = ""
set SPR_FRLRefOrPreref = ""
set SPR_WhichStackAll = ""
set SPR_FRLCycle = ""
set SPR_FRLRef = ""
set SPR_FRLRemovePar = ""
set SPR_FRLRemoveMrc = ""
set SPR_CleanSPR = ""
set SPR_PREREFINE_DIR = ""
set SPR_REFINE_DIR = ""

#
set SPR_FREALIGN_DIR = ${SPR_DIR}/FREALIGN
set SPR_PREREFINE_DIR = ${SPR_FREALIGN_DIR}/${SPR_PREREFINE_DIR}/
set SPR_REFINE_DIR = ${SPR_FREALIGN_DIR}/${SPR_REFINE_DIR}/
#$end_vars

if ( -e LOGS/${scriptname}.results ) then
	mv LOGS/${scriptname}.results LOGS/${scriptname}.results.old
endif

if ( ${SPR_CleanSPR} == "y" ) then

	if ( ${SPR_WhichStackAll} == "non-corrected" ) then
		set stack_name = "particles"
	else
		set stack_name = "particles_${SPR_WhichStackAll}"
	endif

	# Form the strings:

	if ( ${SPR_FRLRefOrPreref} == "refine" ) then
		set SPR_FRLRefOrPreref = ${SPR_REFINE_DIR}
	else
		set SPR_FRLRefOrPreref = ${SPR_PREREFINE_DIR}
	endif

	# if ( ${SPR_FRLRef} == 0 ) then
	# 	set remove_file = "${SPR_FRLRefOrPreref}/_${SPR_FRLCycle}_r*"
	# else
	# 	set remove_file = "${SPR_FRLRefOrPreref}/_${SPR_FRLCycle}_r${SPR_FRLRef}"
	# endif

	if ( ${SPR_FRLRef} == 0 ) then
		set SPR_FRLRef = "*"
	endif

	if ( ${SPR_FRLCycle} == 0 ) then
		set SPR_FRLCycle = "*"
	endif

	set remove_file = "${SPR_FRLRefOrPreref}/${stack_name}_*"

	if ( ${SPR_FRLRemovePar} == "y" ) then
		rm ${remove_file}_${SPR_FRLCycle}_r${SPR_FRLRef}.par
	endif

	if ( ${SPR_FRLRemoveMrc} == "y" ) then
		rm ${remove_file}.mrc
	endif

else if ( ${SPR_CleanSPR} == "n" ) then

	echo ":: Nothing done."
	endif

endif
